<?php
class Controller_Account extends Controller_Template
{
    public function __construct ($req, $res)
    {
        $this->template = 'smarty:account/list';
        parent::__construct($req, $res);
        
    }
    
    public function action_excel_emulator ()
    {
        ini_set('max_execution_time', 300);
        ini_set('memory_limit', '1024M');
            
        $title = array(
            '日期',
            'CPA',
            'CPZ',
            'CPPA',
            '收入预测',
        );
        
        $items = Session::instance()->get('result_emulate1'); 
        $data = array();
        foreach ($items['list'] as  $key => $rs)
        {
            $data1 = array();
            $data1[] = $key;
            $data1[] = $rs['CPA'];
            $data1[] = $rs['CPZ'];
            $data1[] = $rs['CPPA'];
            $data1[] =  sprintf('%.2f', $rs['money']/100);
            $data[] = $data1;
        }
        
        $data2 = array();
        $data2[] = '总计';
        $data2[] = $items['sum']['CPA'];
        $data2[] = $items['sum']['CPZ'];
        $data2[] = $items['sum']['CPPA'];
        $data2[] = sprintf('%.2f', $items['sum']['money']/100);
        $data[] = $data2;
        tools_excel::export2($data, $title, 'emulator'); 
    }
    
   //
    //收入预算明细
    //
    public function action_emulate_day($day = NULL)
    {
        $data = Account::emulate_day($day);
//        var_dump($data);exit;
        $this->template->set_filename('smarty:account/money/detail');
        $this->template->set('items', $data);
    }

    //
    // 收入测算
    //
    public function action_money_emulate ($days = NULL)
    {
        $start_day = $_GET['start_date'];
        $stop_day  = $_GET['end_date'];
       
         if($days == NULL){
            $stop_day  = empty($stop_day)?date('Ymd', time()):date('Ymd', strtotime($stop_day));
            $start_day = empty($start_day)?date('Ymd', time()-86400*31):date('Ymd', strtotime($start_day));
        }  else {
            $stop_day  = empty($stop_day)?date('Ymd', time()):date('Ymd', strtotime($stop_day));
            $start_day = empty($start_day)?date('Ymd', time()-86400*$days+86400):date('Ymd', strtotime($start_day));
        }
        
        $data = account::money_emulate($start_day, $stop_day);
//        var_dump($data);exit;
        $line = account::build_line($data['list']);
        Session::instance()->set('result_emulate1', $data); 
        $this->template->set_filename('smarty:account/money/list');
        $this->template->set('items', $data);
        $this->template->set('line', $line);
        $this->template->set('end_date', date("Y/m/d", strtotime($stop_day)));
        $this->template->set('start_date',date("Y/m/d", strtotime($start_day)));
    }

    public function action_rom ($type = 'show', $uid = NULL, $val = NULL)
    {
        if ($uid > 0)
            $this->template->set('account', account::userinfo($uid));


        if ($type == 'manage_user_rom')
        // 管理员提交上传这个用户的rom文件地址, apk.list和md5内容
        {
        }
        else if($type == 'verify_rom')
        {
            $post = $this->request->post();
            if (! empty($post))
            {
                $rompath  = Arr::get($post, 'rompath');
                $rompass = Arr::get($post, 'rompass');
                $apklistfile  = Arr::get($_FILES, 'apklistfile');
                $md5txtfile  = Arr::get($_FILES, 'md5txtfile');

                $apk_info = array(
                    'apklist' => $apklistfile,
                    'md5txt'  => $md5txtfile,
                    'rompath' => $rompath,
                    'rompass' => $rompass,
                );

                account::rom('update_rom', $uid, $apk_info);
                $this->request->redirect('/account/rom/manage_request_rom');
            }
            else
            {
                $items = Account:: rom($type, $uid);
                $this->template->set('items', $items);
                $this->template->set('id', $uid);
                $this->template->set_filename('smarty:account/rom/verify_rom');
            }
        }
        else if ($type == 'manage_request_rom')
        //ROM申请列表
        {
            $items = Account::rom($type, NULL);
            $this->template->set('items', $items);
            $this->template->set_filename('smarty:account/rom/request_rom');
        }
        else if($type == 'add_apk_system')
        {
            $post = $this->request->post();
            if (! empty($post))
            {
                $appname  = Arr::get($post, 'appname');
                $pkgname  = Arr::get($post, 'pkgname');
                $filename  = Arr::get($post, 'filename');
                if(empty($pkgname)) throw new Exception("包名不能为空");
                $comments = Arr::get($post, 'comments');
                $appfile  = Arr::get($_FILES, 'appfile');
                $apppath  = Arr::get($appfile, 'tmp_name');
                $filesize = Arr::get($appfile, 'size', 0);
                $feetype  = Arr::get($post, 'feetype', 'CPA');
                $price    = Arr::get($post, 'price', '0');
                
                if ($filesize <= 0)
                {
                    throw new Exception("文件不能超过20M");
                }

                $apk_info = array(
                    'appname'  => $appname,
		    'pkgname'  => $pkgname,
                    'filename'  => $filename,
                    'appfile'  => $apppath,
                    'comments' => $comments,
                    'feetype'  => $feetype,
                    'price'  => $price*100,
                );
                
                account::rom('add_apk', '0', $apk_info);
                $this->request->redirect('/account/rom/request_build_system');
            }
            else
            {
                $this->template->set_filename('smarty:account/rom/system/add_apk');
            }
        }
        else if($type == 'request_build_system')
         //制作新的系统ROM
        {
            $post = $this->request->post(); 
            if (! empty($post))
            {
                $apks  = Arr::get($post, 'apk', array());
                account::rom('request_build', 0, $apks);
                $this->request->redirect('/account/rom/manage_system_rom');
            }
            else
            {
                $apks = account::apks(0); 
                $this->template->set('apks', $apks);
            }

            $this->template->set_filename('smarty:account/rom/system/request_build');
        }
        else if ($type == 'manage_system_rom')
        //系统ROM管理
        {
            $info  = account::rom('show', 0);
            $this->template->set('info', $info);
            $this->template->set_filename('smarty:account/rom/system/show');
        }
        else if ($type == 'manage_common_rom')
        // 管理员提交公共ROM文件地址, apk.list和md5内容
        {
            $this->template->set('rom', account::rom($uid));
            $this->template->set_filename('smarty:account/rom/index');
        }
        else if ($type == 'add_apk')
        // 用户提交自己得APK文件
        {
            $post = $this->request->post();
            if (! empty($post))
            {
                $appname  = Arr::get($post, 'appname');
                $pkgname  = Arr::get($post, 'pkgname');
                $filename  = Arr::get($post, 'filename');
                if(empty($pkgname)) throw new Exception("包名不能为空");
                $comments = Arr::get($post, 'comments');
                $appfile  = Arr::get($_FILES, 'appfile');
                $apppath  = Arr::get($appfile, 'tmp_name');
                $filesize = Arr::get($appfile, 'size', 0);
                $feetype  = Arr::get($post, 'feetype', 'CPA');
                $price    = Arr::get($post, 'price', '0');
                
                if ($filesize <= 0)
                {
                    throw new Exception("文件不能超过20M");
                }

                $apk_info = array(
                    'appname'  => $appname,
                    'pkgname'  => $pkgname,
                    'filename'  => $filename,
                    'appfile'  => $apppath,
                    'comments' => $comments,
                    'feetype'  => $feetype,
                    'price'    => $price*100,
                );

                account::rom($type, $uid, $apk_info);
                $this->request->redirect('/account/rom/request_build/'.$uid);
            }
            else
            {
                $this->template->set_filename('smarty:account/rom/add_apk');
            }
        }
        else if ($type == 'request_build')
        // 用户提交自己得rom包内的应用列表，申请由管理员制作ROM包
        {
            $post = $this->request->post(); 
            if (! empty($post))
            {
                $apks  = Arr::get($post, 'apk', array());
                account::rom($type, $uid, $apks);
                $this->request->redirect('/account/rom/show/'.$uid);
            }
            else
            {
                $apks = account::apks($uid); 
                $this->template->set('apks', $apks);
            }

            $this->template->set_filename('smarty:account/rom/request_build');
        }
        else if ($type == 'show')
        // 查看用户当前的rom包的信息
        {
            $info  = account::rom($type, $uid);
            $this->template->set('info', $info);
            $this->template->set_filename('smarty:account/rom/show');
        }
    }

    // apks list
    //
    public function action_apks ($uid)
    {
        $apks = account::apks($uid); 
        $this->template->set_filename('smarty:account/rom/apks');
        $this->template->set('account', account::info($uid));
        $this->template->set('apks', $apks);
    }

    // 账户列表
    public function action_list ($type = NULL)
    {
        $accounts = Account::lists($type);
        $userdetail = account::userinfo(user::current_user_id());
        $userinfo = user::current_user_info();
        $this->template->set_filename('smarty:account/list');
        $this->template->set('utype', $userinfo['utype']);
        $this->template->set('parent', $userdetail['parent']);
        $this->template->set('account_type', $userdetail['account_type']);
        $this->template->set('items', $accounts);
    }

    // 子账户列表
    public function action_children($uid)
    {
        $accounts = Account::children($uid);
        $userinfo = user::current_user_info();
        $this->template->set_filename('smarty:account/children');
        $this->template->set('account', account::info($uid));
        $this->template->set('utype',$userinfo['utype']);
        $this->template->set('items', $accounts);
    }

    public function action_add_channel($uid)
    {
        $account= account::info($uid);
        $user   = user::current_user();
        $auid   = $user->get_user_id();
        $role   = $user->get_role_id();
        if ($role == 'merchant' && $auid != $uid)
        {
            throw new Exception("没有权限");
        }

        $post = $this->request->post();
        if (! empty($post))
        {
	        $user_info = array(
	            'mobile'   => trim(Arr::get($post, 'mobile')),
	            'nickname' => trim(Arr::get($post, 'nickname')),
	            'realname' => trim(Arr::get($post, 'realname')),
	            'comments' => trim(Arr::get($post, 'comments')),
                'parentuid'=> (int) $uid,
                'account_type' => Arr::get($account, 'account_type'),
	        );
	
	        $errstr = "";
	        if (empty($user_info['mobile']))
	            $errstr = "渠道联系电话不能为空";
	        else if (empty($user_info['nickname']))
	            $errstr = "渠道名称不能为空";
	        else if (empty($user_info['realname']))
	            $errstr = "渠道联系人不能为空";
	
	        if (empty($errstr))
	        {
                if (Account::create_channel($user_info))
                {
                    $this->request->redirect('/account/children/'.$uid);
                    return;
                }
                else
                {
                    $errstr = '创建新渠道失败，请重试';
                }
	        }

            $this->template->set('errmsg', $errstr);
        }

        $this->template->set('account', $account);
        $this->template->set_filename('smarty:account/add_channel');
    }
}
 
